﻿Imports VN.INS.BL.BC
Imports System.Data
Imports System.IO
Imports Microsoft.Office.Interop
Imports System.Globalization

Partial Class Devida_PestañaInformeMensualMonitoreo
    Inherits System.Web.UI.Page

    Dim dtcomentario As New DataTable
    Dim dtcomentariofinanciero As New DataTable

#Region "EVENTOS"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not (Page.IsPostBack)) Then
            Me.TabContainer1.ForeColor = Drawing.Color.RoyalBlue

            'By Tigrito(25/06/2013): No es necesario un combo para el periodo puesto que se obtiene de la lista de planes mensuales, se trae como parámetro
            'ListaPeriodo()

            Session("dtcodigo") = Nothing
            Session("dtcomentario") = Nothing
            Session("dtcodigofin") = Nothing
            Session("dtcomentariofinanciero") = Nothing

            'Capturamos parámetros y llenamos datos principales
            ViewState("POA") = Request.QueryString("POACOD")
            ViewState("VSperfil") = Session("perfil").ToString
            ViewState("PERSONA") = Request.QueryString("PERSONA")
            ViewState("VS_Estado_Inf_Mensual") = Request.QueryString("Est_Inf_Mensual") 'Estado del informe mensual

            'By Tigrito (14/08/2013)
            ViewState("VS_VersionPoaVigente") = Request.QueryString("Version_Poa_Vigente") 'Version vigente del POA
            ViewState("VS_VersionPoaInforme") = Request.QueryString("Version_Poa_Informe") 'Version del POA con que se hizo el informe

            txtPPER.Text = Request.QueryString("PPER")
            txtPPER.Enabled = False

            txtproyectoact.Text = Request.QueryString("NombrePOA")
            txtproyectoact.Enabled = False

            txtoz.Text = Request.QueryString("OZ")
            txtoz.Enabled = False

            txteje.Text = Request.QueryString("EJECUTOR")
            txteje.Enabled = False

            txtsnip.Text = Request.QueryString("SNIP")
            txtsnip.Enabled = False

            Select Case ViewState("VS_Estado_Inf_Mensual")
                Case "APROBADO"
                    rbAprob_Inf_Mensual.SelectedValue = "2"
                    chkTerminado.Checked = True
                Case "OBSERVADO"
                    rbAprob_Inf_Mensual.SelectedValue = "3"
            End Select

            'ddl_PERIODO.SelectedValue = Request.QueryString("PERIODO").ToString

            ViewState("Cod_Periodo") = Request.QueryString("Cod_Periodo")
            ViewState("Desc_Periodo") = Request.QueryString("Desc_Periodo")

            ViewState("IDIM") = Request.QueryString("IDIM")
            ViewState("VSIDINFORME") = ViewState("IDIM")
            ViewState("NUEVO") = Request.QueryString("Nuevo")

            txtPeriodo.Text = ViewState("Desc_Periodo") 'ddl_PERIODO.SelectedItem.Text
            txtPeriodo.Enabled = False

            If ViewState("VS_VersionPoaInforme") = "" Then ' ViewState("VS_VersionPoaInforme") es "" solo cuando se ingresa el informe (se crea por primera vez)
                ViewState("VS_VersionPoaInforme") = ViewState("VS_VersionPoaVigente")
            End If

            'By Tigrito(16/08/2013): Carga de la Grilla Física
            Carga_Grilla_Fisica(ViewState("POA"), ViewState("Cod_Periodo"), ViewState("VS_VersionPoaInforme"))

            'By Tigrito(16/08/2013): Carga de la Grilla Financiera
            Carga_Grilla_Financiera(ViewState("POA"), ViewState("Cod_Periodo"), ViewState("VS_VersionPoaInforme"))

            If ViewState("NUEVO") = "1" Then    'Nuevo
                Dim dtcomentario As New DataTable
                dtcomentario.Columns.Add("ID_COMENTARIO_MONITOREO")
                dtcomentario.Columns.Add("ID_INFORME_MENSUAL")
                dtcomentario.Columns.Add("DESCRIPCION")
                Dim row As DataRow = dtcomentario.NewRow()
                row("DESCRIPCION") = ""
                dtcomentario.Rows.Add(row)
                Session("dtcomentario") = dtcomentario
                gv_comentario_fisico.DataSource = dtcomentario
                gv_comentario_fisico.DataBind()

                Dim dtcomentariofinanciero As New DataTable
                dtcomentariofinanciero.Columns.Add("ID_COMENTARIO_MONITOREO")
                dtcomentariofinanciero.Columns.Add("ID_INFORME_MENSUAL")
                dtcomentariofinanciero.Columns.Add("DESCRIPCION")
                Dim rowfinanciero As DataRow = dtcomentariofinanciero.NewRow()
                rowfinanciero("DESCRIPCION") = ""
                dtcomentariofinanciero.Rows.Add(rowfinanciero)
                Session("dtcomentariofinanciero") = dtcomentariofinanciero
                gv_comentario_financiero.DataSource = dtcomentariofinanciero
                gv_comentario_financiero.DataBind()

            ElseIf ViewState("NUEVO") = "2" Or ViewState("VS_Estado_Inf_Mensual") = "TERMINADO" Then    'Edicion
                Dim DT As New DataTable
                Dim OBJ As New PestanaInformeMensualMonitoreoBC
                DT = OBJ.CargarObservaciones(ViewState("IDIM"))
                If DT.Rows.Count = 1 Then
                    txtObs.Text = DT.Rows(0).Item("OBSERVACIONES").ToString
                Else
                    txtObs.Text = ""
                End If
                If txtObs.Text.Length > 50 Then
                    txtObs.Height = txtObs.Text.Length / 4.5
                End If

                'CARGAR COMENTARIO FISICO
                Dim objCGCBC As PestanaInformeMensualMonitoreoBC
                Dim objDtcg, objDtcg1 As New DataTable
                objCGCBC = New PestanaInformeMensualMonitoreoBC
                objDtcg = objCGCBC.CargarGrillaComentario(ViewState("IDIM"), "CFIS")
                If objDtcg.Rows.Count = 0 Then
                    Dim dtcomentario As New DataTable
                    dtcomentario.Columns.Add("ID_COMENTARIO_MONITOREO")
                    dtcomentario.Columns.Add("ID_INFORME_MENSUAL")
                    dtcomentario.Columns.Add("DESCRIPCION")
                    Dim row As DataRow = dtcomentario.NewRow()
                    row("ID_COMENTARIO_MONITOREO") = ""
                    row("ID_INFORME_MENSUAL") = ""
                    row("DESCRIPCION") = ""
                    dtcomentario.Rows.Add(row)
                    Session("dtcomentario") = dtcomentario
                    gv_comentario_fisico.DataSource = dtcomentario
                    gv_comentario_fisico.DataBind()
                Else
                    gv_comentario_fisico.DataSource = objDtcg
                    Session("dtcodigo") = objDtcg
                    objDtcg1 = objCGCBC.CargarGrillaComentario(ViewState("IDIM"), "CFIS")
                    Session("dtcomentario") = objDtcg1
                    gv_comentario_fisico.DataBind()
                End If
                ' CARGAR COMENTARIO FINANCIERO
                Dim objCGCFINBC As PestanaInformeMensualMonitoreoBC
                Dim objDtcgFIN, objDtcg2 As New DataTable
                objCGCFINBC = New PestanaInformeMensualMonitoreoBC
                objDtcgFIN = objCGCFINBC.CargarGrillaComentarioFn(ViewState("IDIM"), "CFIN")
                If objDtcgFIN.Rows.Count = 0 Then
                    Dim dtcomentario As New DataTable
                    dtcomentario.Columns.Add("ID_COMENTARIO_MONITOREO")
                    dtcomentario.Columns.Add("ID_INFORME_MENSUAL")
                    dtcomentario.Columns.Add("DESCRIPCION")
                    Dim row As DataRow = dtcomentario.NewRow()
                    row("ID_COMENTARIO_MONITOREO") = ""
                    row("ID_INFORME_MENSUAL") = ""
                    row("DESCRIPCION") = ""
                    dtcomentario.Rows.Add(row)
                    Session("dtcomentariofinanciero") = dtcomentario
                    gv_comentario_financiero.DataSource = dtcomentario
                    gv_comentario_financiero.DataBind()
                Else
                    gv_comentario_financiero.DataSource = objDtcgFIN
                    Session("dtcodigofin") = objDtcgFIN
                    objDtcg2 = objCGCFINBC.CargarGrillaComentarioFn(ViewState("IDIM"), "CFIN")
                    Session("dtcomentariofinanciero") = objDtcg2
                    gv_comentario_financiero.DataBind()
                End If

                If ViewState("VS_Estado_Inf_Mensual") = "TERMINADO" Then
                    gv_comentario_fisico.Enabled = False
                    gv_comentario_financiero.Enabled = False
                    txtObs.Enabled = False
                    pnlJefe.Visible = True
                    pnlJefe.Enabled = False

                    'Botones agrega y quita comentario físico
                    btn_mas_fis.Visible = False
                    btn_menos_fis.Visible = False
                    'Botones agrega y quita comentario financiero
                    btn_mas_fin.Visible = False
                    btn_menos_fin.Visible = False
                    'Botón Guardar General
                    btn_guard.Visible = False
                End If

            End If

            'By Tigrito(08/07/2013): Visibilidad de botones
            If ViewState("VSperfil") = "MO" Then
                Select Case ViewState("VS_Estado_Inf_Mensual")
                    Case "TERMINADO"
                        Bloquear_Consultar()
                    Case ""
                        Habilitado_Editar()
                    Case "INICIADO"
                        Habilitado_Editar()
                    Case "OBSERVADO"
                        Habilitado_Editar()
                    Case "APROBADO"
                        Bloquear_Consultar()
                    Case Else
                        Bloquear_Consultar()
                End Select
            ElseIf ViewState("VSperfil") = "EP" Then
                Select Case ViewState("VS_Estado_Inf_Mensual")
                    Case "TERMINADO"
                        Bloquear_Consultar()
                        'Panel de los radiobuttons
                        pnlJefe.Visible = True
                        pnlJefe.Enabled = True
                        btn_guard.Visible = True
                    Case "INICIADO"
                        Bloquear_Consultar()
                    Case "OBSERVADO"
                        Bloquear_Consultar()
                    Case "APROBADO"
                        Bloquear_Consultar()
                    Case Else
                        Bloquear_Consultar()
                End Select

            ElseIf ViewState("VSperfil") = "JO" Then
                Select Case ViewState("VS_Estado_Inf_Mensual")
                    Case "TERMINADO"
                        Bloquear_Consultar()
                        'Panel de los radiobuttons
                        pnlJefe.Visible = True
                        pnlJefe.Enabled = True
                        btn_guard.Visible = True
                    Case "INICIADO"
                        Bloquear_Consultar()
                    Case "OBSERVADO"
                        Bloquear_Consultar()
                    Case "APROBADO"
                        Bloquear_Consultar()
                    Case Else
                        Bloquear_Consultar()
                End Select
            Else 'Para el resto de perfiles
                Bloquear_Consultar()
            End If
        End If
    End Sub

    Protected Sub btn_cerrar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_cerrar.Click
        'Redirecciona a lista de informes
        Response.Redirect("InformeMensual.aspx")
    End Sub

    Protected Sub btn_guard_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_guard.Click
        Dim idifisico As Integer
        Dim dtcodigo As New DataTable
        Dim dtcodigofin As New DataTable
        dtcodigo = Session("dtcodigo")
        dtcodigofin = Session("dtcodigofin")
        ViewState("IDIM") = ViewState("VSIDINFORME")

        'EDICION
        If ViewState("NUEVO") = "2" Or ViewState("VS_Estado_Inf_Mensual") = "TERMINADO" Then
            Dim ObjInforme As PestanaInformeMensualMonitoreoBC
            ObjInforme = New PestanaInformeMensualMonitoreoBC
            Try
                'By Tigrito (14/08/2013): No es neceario incluir la version del POA del informe pues el IDIM es único
                ObjInforme.EditarInforme(ViewState("IDIM"), txtObs.Text)
            Catch ex As Exception
                alerta.Mostrar_Alerta("Aviso", ex.Message())
            End Try

            'COMENTARIO FISICO
            Dim i As Integer
            Dim k As Integer
            Dim j As Integer
            If Not (Session("dtcodigo") Is Nothing) Then
                For i = 0 To dtcodigo.Rows.Count - 1
                    k = 0
                    'COMPARAMOS SI SE ELIMINO DATO DE GRILLA
                    For j = 0 To gv_comentario_fisico.Rows.Count - 1
                        Dim HF As HiddenField = CType(gv_comentario_fisico.Rows(j).FindControl("IDCM"), HiddenField)
                        If dtcodigo.Rows(i).Item("ID_COMENTARIO_MONITOREO").ToString = HF.Value.ToString Then
                            k = k + 1
                        End If
                    Next
                    'SI SE ELIMINO DE GRILLA=> ELIMINAMOS DE BD
                    If k = 0 Then
                        eliminarComentario(dtcodigo.Rows(i).Item("ID_COMENTARIO_MONITOREO"))
                    End If
                Next
            End If
            For i = 0 To gv_comentario_fisico.Rows.Count - 1
                'BUSCAMOS CODIGO COMENTARIO
                Dim DESCR As TextBox = CType(gv_comentario_fisico.Rows(i).FindControl("txt_comentariofisico"), TextBox)
                Dim htxtIDCM As HiddenField = CType(gv_comentario_fisico.Rows(i).FindControl("IDCM"), HiddenField)
                'SI EXISTE SE EDITA EL COMENTARIO
                If htxtIDCM.Value <> "" Then
                    ViewState("IDCM") = CInt(htxtIDCM.Value.ToString)
                    Editacomentario(ViewState("IDCM"), ViewState("IDIM"), "CFIS", DESCR.Text)
                Else
                    'SINO EXISTE
                    Dim objgencodifs As GeneralBC
                    objgencodifs = New GeneralBC
                    'SE GENERA EL CODIGO
                    idifisico = objgencodifs.GenerarCodigoNumero("COMENTARIO_MONITOREO", "ID_COMENTARIO_MONITOREO")
                    If Len(DESCR.Text) > 0 Then ' SI SE INGRESO TEXTO SE INSERTA
                        Insertarcomentario(idifisico, "CFIS", DESCR.Text, ViewState("IDIM"))
                    End If
                End If
            Next

            'COMENTARIO FINANCIERO
            Dim n As Integer
            Dim z As Integer
            Dim h As Integer
            If Not (Session("dtcodigofin") Is Nothing) Then
                For n = 0 To dtcodigofin.Rows.Count - 1
                    z = 0
                    'COMPARAMOS SI SE ELIMINO DATO DE GRILLA
                    For h = 0 To gv_comentario_financiero.Rows.Count - 1
                        Dim HFin As HiddenField = CType(gv_comentario_financiero.Rows(h).FindControl("IDCM"), HiddenField)
                        If dtcodigofin.Rows(n).Item("ID_COMENTARIO_MONITOREO").ToString = HFin.Value.ToString Then
                            z = z + 1
                        End If
                    Next
                    'SI SE ELIMINO DE GRILLA=> ELIMINAMOS DE BD
                    If z = 0 Then
                        eliminarComentario(dtcodigofin.Rows(n).Item("ID_COMENTARIO_MONITOREO"))
                    End If
                Next
            End If
            For n = 0 To gv_comentario_financiero.Rows.Count - 1
                'BUSCAMOS CODIGO COMENTARIO
                Dim DESCRfin As TextBox = CType(gv_comentario_financiero.Rows(n).FindControl("txt_comentariofinanciero"), TextBox)
                Dim htxtIDCM As HiddenField = CType(gv_comentario_financiero.Rows(n).FindControl("IDCM"), HiddenField)
                'SI EXISTE SE EDITA EL COMENTARIO
                If htxtIDCM.Value <> "" Then
                    ViewState("IDCM") = CInt(htxtIDCM.Value.ToString)
                    Editacomentario(ViewState("IDCM"), ViewState("IDIM"), "CFIN", DESCRfin.Text)
                Else
                    'SINO EXISTE
                    Dim objgencodifs As GeneralBC
                    objgencodifs = New GeneralBC
                    'SE GENERA EL CODIGO
                    idifisico = objgencodifs.GenerarCodigoNumero("COMENTARIO_MONITOREO", "ID_COMENTARIO_MONITOREO")
                    If Len(DESCRfin.Text) > 0 Then ' SI SE INGRESO TEXTO SE INSERTA
                        Insertarcomentario(idifisico, "CFIN", DESCRfin.Text, ViewState("IDIM"))
                    End If
                End If
            Next

            If ViewState("VSperfil") = "MO" Then
                If ViewState("VS_Estado_Inf_Mensual") = "INICIADO" Or ViewState("VS_Estado_Inf_Mensual") = "OBSERVADO" Then

                    If chkTerminado.Checked = True Then 'Sí checkbox esta marcado es estado 2 sino 1
                        'Guardamos Estado,fecha_aprobación jefe y observacion jefe del informe mensual
                        Dim objMonitorBC As MONITOREOBC
                        objMonitorBC = New MONITOREOBC
                        objMonitorBC.AgregarRevisionInformeMensual(ViewState("IDIM"), ViewState("Cod_Periodo"), "", "1")
                        btn_guard.Visible = False
                        'Response.Redirect("PlanTrabajo.aspx?Nuevo=2&Codigo=" & ViewState("Codigo") & "&Periodo=" & ddl_mes.SelectedValue.ToString & "&Estado=2&Persona=" & ddl_monitoreador.SelectedValue.ToString & "&PPER=" & ViewState("PPER") & "")
                    End If

                End If

            End If

            If ViewState("VSperfil") = "EP" Then
                If ViewState("VS_Estado_Inf_Mensual") = "TERMINADO" Then

                    'Guardamos Estado,fecha_aprobación jefe y observacion jefe del informe mensual
                    Dim objMonitorBC As MONITOREOBC
                    objMonitorBC = New MONITOREOBC
                    objMonitorBC.AgregarRevisionInformeMensual(ViewState("IDIM"), ViewState("Cod_Periodo"), "", rbAprob_Inf_Mensual.SelectedValue.ToString)
                    btn_guard.Visible = False
                    'Response.Redirect("PlanTrabajo.aspx?Nuevo=2&Codigo=" & ViewState("Codigo") & "&Periodo=" & ddl_mes.SelectedValue.ToString & "&Estado=2&Persona=" & ddl_monitoreador.SelectedValue.ToString & "&PPER=" & ViewState("PPER") & "")
                End If
            End If

            If ViewState("VSperfil") = "JO" Then
                If ViewState("VS_Estado_Inf_Mensual") = "TERMINADO" Then

                    'Guardamos Estado,fecha_aprobación jefe y observacion jefe del informe mensual
                    Dim objMonitorBC As MONITOREOBC
                    objMonitorBC = New MONITOREOBC
                    objMonitorBC.AgregarRevisionInformeMensual(ViewState("IDIM"), ViewState("Cod_Periodo"), "", rbAprob_Inf_Mensual.SelectedValue.ToString)
                    btn_guard.Visible = False
                    'Response.Redirect("PlanTrabajo.aspx?Nuevo=2&Codigo=" & ViewState("Codigo") & "&Periodo=" & ddl_mes.SelectedValue.ToString & "&Estado=2&Persona=" & ddl_monitoreador.SelectedValue.ToString & "&PPER=" & ViewState("PPER") & "")
                End If
            End If

            alerta.Mostrar_Alerta("Aviso", "Guardado Correctamente")

            'INGRESO
        ElseIf ViewState("NUEVO") = "1" Then
            Dim idInforme As Integer
            Dim objgencod As GeneralBC
            objgencod = New GeneralBC
            'GENERAMOS CODIGO DE INFORME
            idInforme = objgencod.GenerarCodigoNumero("POA_MENSUAL_MONITOREO", "ID_INFORME_MENSUAL")
            ViewState("IDIM") = idInforme
            ViewState("VSIDINFORME") = idInforme

            'INSERTAMOS INFORME
            'Insertarinforme(idInforme, ViewState("POA"), ddl_PERIODO.SelectedValue.ToString, ViewState("PERSONA"), "A", txtObs.Text)
            Insertarinforme(idInforme, ViewState("POA"), ViewState("Cod_Periodo"), ViewState("PERSONA"), "0", txtObs.Text, ViewState("VS_VersionPoaVigente"))
            Dim i As Integer

            'RECORREMOS GRILLA FISICO
            For i = 0 To gv_comentario_fisico.Rows.Count - 1
                Dim objgencodifs As GeneralBC
                objgencodifs = New GeneralBC
                'GENERAMOS CODIGO DE COMENTARIO FISICO
                idifisico = objgencodifs.GenerarCodigoNumero("COMENTARIO_MONITOREO", "ID_COMENTARIO_MONITOREO")
                Dim DESCR As TextBox = CType(gv_comentario_fisico.Rows(i).FindControl("txt_comentariofisico"), TextBox)
                If Len(DESCR.Text) > 0 Then ' SI SE INGRESO TEXTO SE INSERTA
                    Insertarcomentario(idifisico, "CFIS", DESCR.Text, idInforme)
                End If
            Next
            Dim n As Integer

            'RECORREMOS GRILLA FINANCIERO
            For n = 0 To gv_comentario_financiero.Rows.Count - 1
                Dim idifinanciero As Integer
                Dim objgencodifn As GeneralBC
                objgencodifn = New GeneralBC
                'GENERAMOS CODIGO DE COMENTARIO FINANCIERO
                idifinanciero = objgencodifn.GenerarCodigoNumero("COMENTARIO_MONITOREO", "ID_COMENTARIO_MONITOREO")
                Dim DESCR As TextBox = CType(gv_comentario_financiero.Rows(n).FindControl("txt_comentariofinanciero"), TextBox)
                If Len(DESCR.Text) > 0 Then ' SI SE INGRESO TEXTO SE INSERTA
                    Insertarcomentario(idifinanciero, "CFIN", DESCR.Text, idInforme)
                End If
            Next
            ViewState("NUEVO") = "2"

            'By Tigrito (14/08/2013): Como el informe se ha ingresado por primera vez... 
            '...ahora el la Versión Poa del informe... la Versión Vigente del Poa que acaba de ingresarse
            ViewState("VS_VersionPoaInforme") = ViewState("VS_VersionPoaVigente")

        End If

        CargarGrillasComentarios()

    End Sub

    'Comentario físico AGREGAR
    Protected Sub btn_mas_fis_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_mas_fis.Click
        'agregamos una fila vacia para comentario
        Dim i As Integer
        dtcomentario = Session("dtcomentario")
        For i = 0 To gv_comentario_fisico.Rows.Count - 1
            Dim DESCR As TextBox = CType(gv_comentario_fisico.Rows(i).FindControl("txt_comentariofisico"), TextBox)
            dtcomentario.Rows(i)("DESCRIPCION") = DESCR.Text
        Next
        Dim row As DataRow = dtcomentario.NewRow()
        row("DESCRIPCION") = ""
        dtcomentario.Rows.Add(row)
        Session("dtcomentario") = dtcomentario
        gv_comentario_fisico.DataSource = dtcomentario
        gv_comentario_fisico.DataBind()
    End Sub

    'Comentario físico QUITAR
    Protected Sub btn_menos_fis_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_menos_fis.Click
        'eliminamos la ultima fila de comentario
        Dim i As Integer
        dtcomentario = Session("dtcomentario")
        For i = 0 To gv_comentario_fisico.Rows.Count - 1
            Dim DESCR As TextBox = CType(gv_comentario_fisico.Rows(i).FindControl("txt_comentariofisico"), TextBox)
            dtcomentario.Rows(i)("DESCRIPCION") = DESCR.Text
        Next
        dtcomentario.Rows.RemoveAt(dtcomentario.Rows.Count - 1)
        Session("dtcomentario") = dtcomentario
        gv_comentario_fisico.DataSource = dtcomentario
        gv_comentario_fisico.DataBind()
    End Sub

    'Comentario financiero AGREGAR
    Protected Sub btn_mas_fin_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_mas_fin.Click
        'agregamos nueva fila para comentario financiero
        Dim i As Integer
        dtcomentariofinanciero = Session("dtcomentariofinanciero")
        For i = 0 To gv_comentario_financiero.Rows.Count - 1
            Dim DESCR As TextBox = CType(gv_comentario_financiero.Rows(i).FindControl("txt_comentariofinanciero"), TextBox)
            dtcomentariofinanciero.Rows(i)("DESCRIPCION") = DESCR.Text
        Next
        Dim rowfinanciero As DataRow = dtcomentariofinanciero.NewRow()
        rowfinanciero("DESCRIPCION") = ""
        dtcomentariofinanciero.Rows.Add(rowfinanciero)
        Session("dtcomentariofis") = dtcomentariofinanciero
        gv_comentario_financiero.DataSource = dtcomentariofinanciero
        gv_comentario_financiero.DataBind()
    End Sub

    'Comentario financiero QUITAR
    Protected Sub btn_menos_fin_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_menos_fin.Click
        'eiminamos ultimo comentario financiero
        Dim n As Integer
        dtcomentariofinanciero = Session("dtcomentariofinanciero")
        For n = 0 To gv_comentario_financiero.Rows.Count - 1
            Dim DESCR As TextBox = CType(gv_comentario_financiero.Rows(n).FindControl("txt_comentariofinanciero"), TextBox)
            dtcomentariofinanciero.Rows(n)("DESCRIPCION") = DESCR.Text
        Next
        dtcomentariofinanciero.Rows.RemoveAt(dtcomentariofinanciero.Rows.Count - 1)
        Session("dtcomentariofinanciero") = dtcomentariofinanciero
        gv_comentario_financiero.DataSource = dtcomentariofinanciero
        gv_comentario_financiero.DataBind()
    End Sub

    Protected Sub btnWord_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnWord.Click
        ExportWord()
    End Sub


#Region "VARIABLES"

    'By Tigrito (03/07/2013): For the summarize gridview financial
    Dim _totalMetaAnual_IM_Fin As Decimal = 0
    'Mensual
    Dim _totalMetaProgMensual_IM_Fin As Decimal = 0
    Dim _totalEjeMensual_IM_Fin As Decimal = 0

    'Acumulado
    Dim _totalMetaProgMensualAcum_IM_Fin As Decimal = 0
    Dim _totalEjeMensualAcum_IM_Fin As Decimal = 0

#End Region
    
    Protected Sub gv_ifinanciero_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gv_ifinanciero.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            'Dim product As Northwind.ProductsRow = CType(CType(e.Row.DataItem, DataRowView).Row, Northwind.ProductsRow)

            Dim Aux1 As Decimal = 0 'Para el total programado
            Dim Aux2 As Decimal = 0 'Para el programado mensual
            Dim Aux3 As Decimal = 0 'Para el ejecutado mensual
            Dim Aux4 As Decimal = 0 'Para el programado mensual acumulado
            Dim Aux5 As Decimal = 0 'Para el ejecutado mensual acumulado

            If e.Row.Cells(2).Text.ToString = "" Or e.Row.Cells(2).Text.ToString = "&nbsp;" Or IsDBNull(e.Row.Cells(2).Text.ToString) Then
                Aux1 = 0
            Else
                Aux1 = Convert.ToDecimal(e.Row.Cells(2).Text.ToString)
            End If

            If e.Row.Cells(3).Text.ToString = "" Or e.Row.Cells(3).Text.ToString = "&nbsp;" Or IsDBNull(e.Row.Cells(3).Text.ToString) Then
                Aux2 = 0
            Else
                Aux2 = Convert.ToDecimal(e.Row.Cells(3).Text.ToString)
            End If

            If e.Row.Cells(4).Text.ToString = "" Or e.Row.Cells(4).Text.ToString = "&nbsp;" Or IsDBNull(e.Row.Cells(4).Text.ToString) Then
                Aux3 = 0
            Else
                Aux3 = Convert.ToDecimal(e.Row.Cells(4).Text.ToString)
            End If

            If e.Row.Cells(6).Text.ToString = "" Or e.Row.Cells(6).Text.ToString = "&nbsp;" Or IsDBNull(e.Row.Cells(6).Text.ToString) Then
                Aux4 = 0
            Else
                Aux4 = Convert.ToDecimal(e.Row.Cells(6).Text.ToString)
            End If

            If e.Row.Cells(7).Text.ToString = "" Or e.Row.Cells(7).Text.ToString = "&nbsp;" Or IsDBNull(e.Row.Cells(7).Text.ToString) Then
                Aux5 = 0
            Else
                Aux5 = Convert.ToDecimal(e.Row.Cells(7).Text.ToString)
            End If

            _totalMetaAnual_IM_Fin += Aux1

            _totalMetaProgMensual_IM_Fin += Aux2
            _totalEjeMensual_IM_Fin += Aux3
            _totalMetaProgMensualAcum_IM_Fin += Aux4
            _totalEjeMensualAcum_IM_Fin += Aux5

        ElseIf e.Row.RowType = DataControlRowType.Footer Then
            e.Row.Cells(1).Text = "TOTALES"

            e.Row.Cells(2).Text = (_totalMetaAnual_IM_Fin).ToString("#,0.0")

            e.Row.Cells(3).Text = (_totalMetaProgMensual_IM_Fin).ToString("#,0.0")
            e.Row.Cells(4).Text = (_totalEjeMensual_IM_Fin).ToString("#,0.0")

            'Para el porcentaje ejecutado mensual
            If _totalMetaProgMensual_IM_Fin = 0 Then
                e.Row.Cells(5).Text = 0.ToString("#,0.0 %")
            Else
                e.Row.Cells(5).Text = ((_totalEjeMensual_IM_Fin / _totalMetaProgMensual_IM_Fin)).ToString("#,0.0 %")
            End If

            e.Row.Cells(6).Text = (_totalMetaProgMensualAcum_IM_Fin).ToString("#,0.0")
            e.Row.Cells(7).Text = (_totalEjeMensualAcum_IM_Fin).ToString("#,0.0")

            'Para el porcentaje ejecutado mensual acumulado
            If _totalMetaProgMensualAcum_IM_Fin = 0 Then
                e.Row.Cells(8).Text = 0.ToString("#,0.0 %")
            Else
                e.Row.Cells(8).Text = ((_totalEjeMensualAcum_IM_Fin / _totalMetaProgMensualAcum_IM_Fin)).ToString("#,0.0 %")
            End If

            'e.Row.Cells(3).Text = (_totalMetaProgMensual / 2).ToString("#,000.0")
            'e.Row.Cells(4).Text = (_totalMetaProgAcum / 2).ToString("#,000.0")

            'e.Row.Cells(2).Text = (_totalMetaAnual_IM_Fin).ToString("#,0.0")
            'e.Row.Cells(3).Text = (_totalMetaProgMensual_IM_Fin).ToString("#,0.0")
            'e.Row.Cells(4).Text = (_totalMetaProgAcum_IM_Fin).ToString("#,0.0")

        End If
    End Sub

    Protected Sub gv_comentario_fisico_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gv_comentario_fisico.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim txtcom As TextBox = CType(e.Row.Cells(1).FindControl("txt_comentariofisico"), TextBox)
            If txtcom.Text.Length > 50 Then
                txtcom.Height = txtcom.Text.Length / 4.5
            Else
                txtcom.Height = 20
            End If
        End If
    End Sub

    Protected Sub gv_comentario_financiero_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gv_comentario_financiero.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim txtcom As TextBox = CType(e.Row.Cells(1).FindControl("txt_comentariofinanciero"), TextBox)
            If txtcom.Text.Length > 50 Then
                txtcom.Height = txtcom.Text.Length / 4.5
            Else
                txtcom.Height = 20
            End If
        End If
    End Sub


#End Region

#Region "PROCEDIMIENTOS"

    Sub Habilitado_Editar()
        gv_comentario_fisico.Enabled = True
        gv_comentario_financiero.Enabled = True
        txtObs.Enabled = True

        'Botones agrega y quita comentario físico
        btn_mas_fis.Visible = True
        btn_menos_fis.Visible = True
        'Botones agrega y quita comentario financiero
        btn_mas_fin.Visible = True
        btn_menos_fin.Visible = True

        'Botón Guardar General
        btn_guard.Visible = True
    End Sub

    Sub Bloquear_Consultar()
        'Para el check terminado
        chkTerminado.Enabled = False

        gv_comentario_fisico.Enabled = False
        gv_comentario_financiero.Enabled = False
        txtObs.Enabled = False
        'Panel de los radiobuttons
        pnlJefe.Visible = True
        pnlJefe.Enabled = False

        'Botones agrega y quita comentario físico
        btn_mas_fis.Visible = False
        btn_menos_fis.Visible = False
        'Botones agrega y quita comentario financiero
        btn_mas_fin.Visible = False
        btn_menos_fin.Visible = False
        'Botón Guardar General
        btn_guard.Visible = False
    End Sub

    Public Sub ListaPeriodo()
        Dim objPPER As PPERBC
        Dim objData As DataTable
        objPPER = New PPERBC
        objData = New DataTable
        objData = objPPER.ListaPeriodo("PLAN_TRABAJO_INGRESO", Session("Año"))
        ddl_PERIODO.DataSource = objData
        ddl_PERIODO.DataTextField = "DESCRIPCION"
        ddl_PERIODO.DataValueField = "PERIODO"
        ddl_PERIODO.DataBind()
    End Sub

    Sub Carga_Grilla_Fisica(ByVal strPOA As String, ByVal strPeriodo As String, ByVal strVersionPoaInforme As String)
        Dim ObjIF As PestanaInformeMensualMonitoreoBC
        Dim objData As DataTable
        ObjIF = New PestanaInformeMensualMonitoreoBC
        objData = New DataTable

        'Carga de grilla fisico
        objData = ObjIF.Informe_Grilla_Fisico(strPOA, strPeriodo, strVersionPoaInforme)
        gv_ifisica.DataSource = objData
        Session("GrillaFisica") = objData
        gv_ifisica.DataBind()
    End Sub

    Sub Carga_Grilla_Financiera(ByVal strPOA As String, ByVal strPeriodo As String, ByVal strVersionPoaInforme As String)
        Dim ObjIM As PestanaInformeMensualMonitoreoBC
        Dim objData As DataTable
        ObjIM = New PestanaInformeMensualMonitoreoBC
        objData = New DataTable

        'Carga de grilla financiero
        objData = ObjIM.Informe_Grilla_Financiero(strPOA, strPeriodo, strVersionPoaInforme)
        gv_ifinanciero.DataSource = objData
        Session("GrillaFinanciera") = objData
        gv_ifinanciero.DataBind()
    End Sub

    Public Sub Insertarinforme(ByVal IDIM As String, ByVal POAIM As String, ByVal PDOIM As String, ByVal PERSO As String, ByVal EST As String, ByVal OBS As String, ByVal strVersionPoaVigente As String)
        Dim objIIBC As PestanaInformeMensualMonitoreoBC
        Try
            objIIBC = New PestanaInformeMensualMonitoreoBC
            'Inserción de informe
            objIIBC.Insertarinforme(IDIM, POAIM, PDOIM, PERSO, EST, OBS, strVersionPoaVigente)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub Insertarcomentario(ByVal IDCM As String, ByVal TIPOCM As String, ByVal DESCR As String, ByVal IDIM As String)
        Dim objICBC As PestanaInformeMensualMonitoreoBC
        Try
            objICBC = New PestanaInformeMensualMonitoreoBC
            'Insercion de comentario
            objICBC.Insertarcomentario(IDCM, TIPOCM, DESCR, IDIM)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub Editacomentario(ByVal IDCM As String, ByVal IDIM As String, ByVal TIPOCM As String, ByVal DESCR As String)
        Dim objECDA As PestanaInformeMensualMonitoreoBC
        Try
            objECDA = New PestanaInformeMensualMonitoreoBC
            'Edicion de comentario
            objECDA.Editacomentario(IDCM, IDIM, TIPOCM, DESCR)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub eliminarComentario(ByVal IDCM As String)
        Dim objECDA As PestanaInformeMensualMonitoreoBC
        Try
            objECDA = New PestanaInformeMensualMonitoreoBC
            'eliminar comentario

            objECDA.eliminarComentario(IDCM)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub CargarGrillasComentarios()
        ' CARGAR COMENTARIO FISICO
        alerta.Mostrar_Alerta("Aviso", "Informe guardado correctamente")
        Dim objCGCBC As PestanaInformeMensualMonitoreoBC
        Dim objDtcg, objDtcg1 As New DataTable
        objCGCBC = New PestanaInformeMensualMonitoreoBC
        objDtcg = objCGCBC.CargarGrillaComentario(ViewState("IDIM"), "CFIS")

        If objDtcg.Rows.Count = 0 Then
            Dim row As DataRow = objDtcg.NewRow()
            row("DESCRIPCION") = ""
            objDtcg.Rows.Add(row)
        End If
        gv_comentario_fisico.DataSource = objDtcg
        Session("dtcodigo") = objDtcg
        objDtcg1 = objCGCBC.CargarGrillaComentario(ViewState("IDIM"), "CFIS")
        Session("dtcomentario") = objDtcg1
        gv_comentario_fisico.DataBind()

        ' CARGAR COMENTARIO FINANCIERO
        Dim objCGCFINBC As PestanaInformeMensualMonitoreoBC
        Dim objDtcgFIN, objDtcg2 As New DataTable
        objCGCFINBC = New PestanaInformeMensualMonitoreoBC
        objDtcgFIN = objCGCFINBC.CargarGrillaComentarioFn(ViewState("IDIM"), "CFIN")
        If objDtcgFIN.Rows.Count = 0 Then
            Dim rowfinanciero As DataRow = objDtcgFIN.NewRow()
            rowfinanciero("DESCRIPCION") = ""
            objDtcgFIN.Rows.Add(rowfinanciero)
        End If
        gv_comentario_financiero.DataSource = objDtcgFIN
        Session("dtcodigofin") = objDtcgFIN
        objDtcg2 = objCGCFINBC.CargarGrillaComentarioFn(ViewState("IDIM"), "CFIN")
        Session("dtcomentariofinanciero") = objDtcg2
        gv_comentario_financiero.DataBind()
    End Sub

    Protected Sub ExportWord()
        Dim gvtitle1 As New Table
        Dim titulo1 As New TableCell()
        titulo1.RowSpan = 1
        titulo1.Text = "INFORME MENSUAL DE MONITOREO"
        titulo1.Font.Name = "Calibri"
        titulo1.Font.Size = 13
        titulo1.Font.Bold = True
        titulo1.BackColor = Drawing.Color.White
        titulo1.HorizontalAlign = HorizontalAlign.Center
        titulo1.VerticalAlign = VerticalAlign.Middle
        Dim headertitulo1 As New TableRow()
        headertitulo1.Cells.Add(titulo1)
        gvtitle1.Rows.Add(headertitulo1)
        Dim Cabecera As String = "<h5 style='text-align:center'>FORMATO 03</h5> " + _
                                "<h5 style='text-align:center'>INFORME MENSUAL DE EJECUCIÓN DE PROYECTOS O ACTIVIDADES</h5> " + _
                                 "<div style='width: 680px; text-align:center'>" + _
                                "<table style='width: 680px; '>" + _
                                "<tr style='font-weight: bold; font-size: 9pt;'><td style='width: 130px; '>PPER: </td><td style='font-size:9pt; font-weight:normal;'>" + txtPPER.Text + "</td></tr>" + _
                                "<tr style='font-weight: bold; font-size: 9pt;'><td> PROYECTO / ACTIVIDAD: </td><td style='font-size:9pt; font-weight:normal;''>" + Server.HtmlDecode(txtproyectoact.Text) + "</td></tr>" + _
                                "<tr style='font-weight: bold; font-size: 9pt;'><td> OFICINA ZONAL: </td><td style='font-size:9pt; font-weight:normal;'>" + txtoz.Text + "</td></tr>" + _
                                "</table>" + _
                                "</div><br>" + _
                                "<div style='width: 680px; text-align:center'>" + _
                                "<table style='width: 680px; '>" + _
                                "<tr style='font-weight: bold; font-size: 10pt;'><td> 1. </td><td colspan='2' style='width: 130px; '>" + Server.HtmlDecode("INFORMACION GENERAL: ") + "</td></tr>" + _
                                "<tr><td></td><td style='font-size: 11pt'>Entidad Ejecutora: </td><td style='font-size: 11pt'>" + Server.HtmlDecode(txteje.Text) + "</td></tr>" + _
                                "<tr><td></td><td style='font-size: 11pt'>Código SNIP: </td><td  style='font-size: 11pt'>" + txtsnip.Text + "</td></tr>" + _
                                "<tr><td></td><td style='font-size: 11pt'>Informe correspondiente al mes de: </td><td style='font-size: 11pt'>" + Server.HtmlDecode(txtPeriodo.Text.Substring(0, 1)) + Server.HtmlDecode(txtPeriodo.Text.Substring(1).ToLower) + "</td></tr>" + _
                                "<tr><td>&nbsp</td><td colspan='2' style='font-size:10pt'><h6> </h6></td></tr>" + _
                                "<tr style='font-weight: bold; font-size: 10pt;'><td> 2. </td><td colspan='2' style='font-size: 10pt'>SITUACION DE LA EJECUCION DE LAS METAS FISICAS</td></tr>" + _
                                "</table>" + _
                                "</div>"

        Dim ChartTable1 As String = "<div style='width: 680px; text-align:center'>" + _
                                    "<table style='border-width: 1px 1px 1px 1px; width: 680px; text-align:center; " + _
                                    "color: #000000; height: 50px; font-size: 9pt; border-color: #000000 #000000 #000000 #000000; " + _
                                    "border-style: solid;' cellpadding='0' cellspacing='0'> " + _
                                    "<tr style='height: 25px text-align:center; background-color: #BBBBBB;'> " + _
                                    "    <td style='width: 30px; border-right: 1px solid #111111;  font-size: 9pt; font-weight: bold; text-align:center;' " + _
                                    "    rowspan = '2' >        Código  " + _
                                    "    </td>  " + _
                                    "    <td style='width: 350px; border-right: 1px solid #111111;  font-size: 9pt; font-weight: bold; text-align:center;' " + _
                                    "    rowspan = '2' >        Componente / Actividad  " + _
                                    "    </td>  " + _
                                    "    <td style='width: 65px; border-right: 1px solid #111111; font-size: 9pt; font-weight: bold; text-align:center;' " + _
                                    "        rowspan='2'>       Unidad de Medida " + _
                                    "    </td> " + _
                                    "    <td style='width: 58px; border-right: 1px solid #111111; font-size: 9pt; font-weight: bold ;text-align:center;' " + _
                                    "        rowspan='2'>        Meta Anual " + _
                                    "    </td> " + _
                                    "    <td style='border-right: 1px solid #111111; border-bottom: 1px solid #111111; font-size: 9pt; font-weight: bold; text-align:center;' colspan='3'> " + _
                                    "        Meta del mes " + _
                                    "    </td> " + _
                                    "    <td style='border-bottom: 1px solid #111111; font-size: 9pt; font-weight: bold; text-align:center;' colspan='3'> " + _
                                    "       Meta Acumulada " + _
                                    "    </td> " + _
                                    "</tr> " + _
                                    "<tr style=' background-color: #BBBBBB;' > " + _
                                    "    <td style='border-right: 1px solid #111111; font-size: 9pt; font-weight: bold; text-align:center;'> " + _
                                    "        Prog. (1) " + _
                                    "    </td> " + _
                                    "    <td style='border-right: 1px solid #111111; font-size: 9pt; font-weight: bold; text-align:center;' > " + _
                                    "        Ejec. (2) " + _
                                    "    </td> " + _
                                    "    <td style='border-right: 1px solid #111111; padding: 0 ; font-size: 9pt; " + _
                                    "        font-weight: bold; text-align:center;' > " + _
                                    "        % Ejec. (2)/(1) " + _
                                    "    </td> " + _
                                    "    <td style='border-right: 1px solid #111111; font-size: 9pt; font-weight: bold; text-align:center;'> " + _
                                    "        Prog. (4) " + _
                                    "    </td> " + _
                                    "    <td style='padding: 0; border-right: 1px solid #111111; font-size: 9pt; font-weight: bold; text-align:center;'> " + _
                                    "        Ejec. (5) " + _
                                    "    </td> " + _
                                    "    <td style='padding: 0; font-size: 9pt; font-weight: bold; text-align:center;'> " + _
                                    "        % Ejec. (5)/(4) " + _
                                    "    </td> " + _
                                    "</tr> " '+ _


        'Cuadro avance físico
        Dim dtFisico As DataTable
        dtFisico = New DataTable
        dtFisico = Session("GrillaFisica")

        Dim ChartTable2 As String = "<div style='width: 680px; text-align:center'>" + _
                                    "<table style='border-width: 1px 1px 1px 1px; width: 680px; font-size: 9pt; " + _
                                    "color: #000000; border-color: #000000 #000000 #000000 #000000; " + _
                                    "border-style: solid;' cellpadding='0' cellspacing='0'> " + _
                                    "     <tr style='height: 25px; font-weight: bold; background-color: #BBBBBB; text-align:center;'>" + _
                                    "         <td style='border-right: 1px solid #111111; border-bottom: 1px solid #111111; font-weight: bold;' colspan='2'>" + _
                                    "             Especifica" + _
                                    "         </td>" + _
                                    "         <td style='width: 50px; border-right: 1px solid #111111; font-weight: bold;'" + _
                                    "             rowspan='2'>" + _
                                    "             Monto Anual S/." + _
                                    "         </td>" + _
                                    "         <td style='border-right: 1px solid #111111; border-bottom: 1px solid #111111; font-weight: bold;' colspan='3'>" + _
                                    "             Monto del mes" + _
                                    "         </td>" + _
                                    "         <td style='border-bottom: 1px solid #111111; font-weight: bold;' colspan='3'>" + _
                                    "       Monto Acumulado" + _
                                    "         </td>" + _
                                    "     </tr>" + _
                                    "     <tr style='background-color: #BBBBBB; text-align:center; font-weight: bold;'>" + _
                                    "         <td style='width: 35px; border-right: 1px solid #111111; font-weight: bold;' > " + _
                                    "        Código " + _
                                    "         </td>" + _
                                    "         <td style='width: 295px; border-right: 1px solid #111111; font-weight: bold;' >" + _
                                    "        Nombre " + _
                                    "         </td>" + _
                                    "         <td style='width: 45px; border-right: 1px solid #111111; font-weight: bold;'>" + _
                                    "             Prog (1) S/." + _
                                    "         </td>" + _
                                    "         <td style='width: 45px; border-right: 1px solid #111111; font-weight: bold;'>" + _
                                    "             Ejec. (2) S/." + _
                                    "         </td>" + _
                                    "         <td style='width: 45px; border-right: 1px solid #111111; font-weight: bold;'>" + _
                                    "             % Ejec. 3=2/1" + _
                                    "         </td>" + _
                                    "         <td style='width: 45px; border-right: 1px solid #111111; font-weight: bold;'>" + _
                                    "             Prog (4) S/." + _
                                    "         </td>" + _
                                    "         <td style='width: 45px; border-right: 1px solid #111111; font-weight: bold;'>" + _
                                    "             Ejec. (5) S/." + _
                                    "         </td>" + _
                                    "         <td style='font-weight: bold; padding:0; '>" + _
                                    "             % Ejec. 6=5/4 " + _
                                    "         </td>" + _
                                    "     </tr> " '+ _

        Dim porc1 As String
        For i As Int16 = 0 To dtFisico.Rows.Count - 1
            If dtFisico.Rows(i).Item("PORCENTAJE").ToString = "" Then
                porc1 = ""
            Else
                porc1 = CDbl(dtFisico.Rows(i).Item("PORCENTAJE")).ToString("##.00 %")
            End If

            ChartTable1 = ChartTable1 + "<tr style='font-size: 9pt;'>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:left; vertical-align:top' >" + dtFisico.Rows(i).Item("CODIGO").ToString + "</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:left; vertical-align:top' >" + dtFisico.Rows(i).Item("TAREA_ACTIVIDAD_DESCRIPCION").ToString + "</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:left; vertical-align:top' >" + dtFisico.Rows(i).Item("DESCRIPCION").ToString + "</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:right; vertical-align:top' >" + CDbl(dtFisico.Rows(i).Item("META_ANUAL")).ToString("#,0.0") + "</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:right; vertical-align:top' >" + CDbl(dtFisico.Rows(i).Item("PROGRAMADO_MES")).ToString("#,0.0") + "</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:right; vertical-align:top' >" + CDbl(dtFisico.Rows(i).Item("EJECUTADO1")).ToString("#,0.0") + "</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:right; vertical-align:top; white-space: nowrap' >" + porc1 + "</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:right; vertical-align:top' >" + CDbl(dtFisico.Rows(i).Item("ACUMULADO")).ToString("#,0.0") + "</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:right; vertical-align:top' >" + CDbl(dtFisico.Rows(i).Item("EJECUTADO")).ToString("#,0.0") + "</td>" + _
                                              "<td style='border-top: 1px solid #111111; text-align:right; vertical-align:top; white-space: nowrap'>" + CDbl(dtFisico.Rows(i).Item("PORCENTAJE2")).ToString("##.00 %") + "</td></tr>"
        Next
        ChartTable1 = ChartTable1 + "</table>" + _
                                "</div>" + _
                                "<pre style='font-size: 9pt; font-family:Times New Roman; text-align:left;'> (*), (1) y (4) Corresponden a Información según el Plan Operativo</pre>"

        'Cuadro avance financiero
        Dim dtFinanciero As DataTable
        dtFinanciero = New DataTable
        dtFinanciero = Session("GrillaFinanciera")
        Dim sum1, sum2, sum3, sum4, sum5, sum6, sum7, porcEjeMensual, porcEjeMensualAcum As Double
        sum1 = 0
        sum2 = 0
        sum3 = 0
        sum4 = 0
        sum5 = 0
        sum6 = 0
        sum7 = 0
        porcEjeMensual = 0
        porcEjeMensualAcum = 0

        For i As Int16 = 0 To dtFinanciero.Rows.Count - 1
            ChartTable2 = ChartTable2 + "<tr style='font-size: 9pt;'><td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:left; vertical-align:top'>" + dtFinanciero.Rows(i).Item("ESPECIFICA_GASTO").ToString + "</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:left; vertical-align:top' >" + dtFinanciero.Rows(i).Item("DESCRIPCION").ToString + "</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:right; vertical-align:top'>" + CDbl(dtFinanciero.Rows(i).Item("META_ANUAL")).ToString("0,0.00") + "</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:right; vertical-align:top'>" + CDbl(dtFinanciero.Rows(i).Item("PROGRAMADO_MES")).ToString("0,0.00") + "</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:right; vertical-align:top'>" + CDbl(dtFinanciero.Rows(i).Item("EJECUTADO1")).ToString("0,0.00") + "</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:right; vertical-align:top; white-space: nowrap'>" + CDbl(dtFinanciero.Rows(i).Item("PORCENTAJE")).ToString("##.00 %") + "</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:right; vertical-align:top'>" + CDbl(dtFinanciero.Rows(i).Item("ACUMULADO")).ToString("0,0.00") + "</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:right; vertical-align:top'>" + CDbl(dtFinanciero.Rows(i).Item("EJECUTADO")).ToString("0,0.00") + "</td>" + _
                                              "<td style='border-top: 1px solid #111111; text-align:right; vertical-align:top; white-space: nowrap'>" + CDbl(dtFinanciero.Rows(i).Item("PORCENTAJE2")).ToString("##.00 %") + "</td></tr>"
            sum1 = sum1 + dtFinanciero.Rows(i).Item("META_ANUAL")

            sum2 = sum2 + dtFinanciero.Rows(i).Item("PROGRAMADO_MES")
            sum3 = sum3 + dtFinanciero.Rows(i).Item("EJECUTADO1")

            sum4 = sum4 + dtFinanciero.Rows(i).Item("PORCENTAJE")

            sum5 = sum5 + dtFinanciero.Rows(i).Item("ACUMULADO")
            sum6 = sum6 + dtFinanciero.Rows(i).Item("EJECUTADO")

            sum7 = sum7 + dtFinanciero.Rows(i).Item("PORCENTAJE2")
        Next

        If sum2 = 0 Then
            porcEjeMensual = 0
        Else
            porcEjeMensual = sum3 / sum2
        End If

        If sum5 = 0 Then
            porcEjeMensualAcum = 0
        Else
            porcEjeMensualAcum = sum6 / sum5
        End If

        ChartTable2 = ChartTable2 + "<tr style='background:#E4E4E4; font-weight:bold; font-size: 9pt;'><td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:center' colspan='2'>TOTAL</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:right' >" + sum1.ToString("0,0.00") + "</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:right'>" + sum2.ToString("0,0.00") + "</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:right'>" + sum3.ToString("0,0.00") + "</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:right'>" + porcEjeMensual.ToString("##.00 %") + "</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:right'>" + sum5.ToString("0,0.00") + "</td>" + _
                                              "<td style='border-right: 1px solid #111111; border-top: 1px solid #111111; text-align:right'>" + sum6.ToString("0,0.00") + "</td>" + _
                                              "<td style='border-top: 1px solid #111111; text-align:right'>" + porcEjeMensualAcum.ToString("##.00 %") + "</td></tr>" + _
                                              "</table>" + _
                                              "</div>" + _
                                              "<pre style='font-size: 9pt; font-family:Times New Roman; text-align:left;'>(*), (1) y (4) Corresponden a Información según el Plan Operativo <br/>(2) Monto ejecutado en el mes reportado, según SIAF consulta amigable a nivel girado. <br/>(3) Monto ejecutado acumulado desde el primer mes de ejecución hasta el mes reportado, según el SIAF consulta amigable a nivel de girado.</pre>"
        'Comentarios Metas físicas
        Dim strComentarioFis As String = "<div style='width: 680px; text-align:center'>" + _
                                    "<table style='border-width: 1px 1px 1px 1px; width: 680px;  " + _
                                    "color: #000000; ' cellpadding='4' cellspacing='0'> " + _
                                    "<tr style='font-weight: bold; font-size: 10pt;'><td style='width:25px;'>2.1</td><td>COMENTARIOS DERIVADOS DEL AVANCE FISICO</td></tr>"


        Dim dtComFis As DataTable
        dtComFis = New DataTable
        dtComFis = Session("dtcomentario")
        For j As Int16 = 0 To dtComFis.Rows.Count - 1
            strComentarioFis = strComentarioFis + "<tr style='vertical-align:top; font-size: 11pt;'><td>2.1." + (j + 1).ToString + "</td><td><PRE style='font-size: 11pt; font-family:Times New Roman; text-align:justify' >" + dtComFis.Rows(j).Item("DESCRIPCION").ToString + "</PRE></td></tr>"
        Next

        strComentarioFis = strComentarioFis + "</table></div>" + _
                                            "<div style='width: 680px; text-align:center'>" + _
                                            "<table style='border-width: 0; width: 680px;' >" + _
                                            "<tr style='font-weight: bold; font-size:10pt; '><td style='width:25px;'>3. </td><td>SITUACION DE LA EJECUCION DE LAS METAS FINANCIERAS</td></tr></table>"
        'Comentarios Metas financieras
        Dim strComentarioFin As String = "<div style='width: 680px; text-align:center'>" + _
                                    "<table style='border-width: 1px 1px 1px 1px; width: 680px; " + _
                                    "color: #000000; ' cellpadding='4' cellspacing='0'> " + _
                                    "<tr style='font-weight: bold; font-size:10pt; '><td style='width:25px;'>3.1</td><td>COMENTARIOS DERIVADOS DEL AVANCE FINANCIERO</td></tr>"


        Dim dtComFin As DataTable
        dtComFin = New DataTable
        dtComFin = Session("dtcomentariofinanciero")
        For k As Int16 = 0 To dtComFin.Rows.Count - 1
            strComentarioFin = strComentarioFin + "<tr style='vertical-align:top; font-size: 11pt; '><td>3.1." + (k + 1).ToString + "</td><td><PRE style='font-size: 11pt; font-family:Times New Roman;  text-align:justify'>" + dtComFin.Rows(k).Item("Descripcion").ToString + "</PRE></td></tr>"
        Next
        strComentarioFin = strComentarioFin + "</table></div>"

        'Dificultades y limitaciones presentadas
        Dim strObservaciones As String = "<div style='width: 680px; text-align:center'>" + _
                                   "<table style='border-width: 1px 1px 1px 1px; width: 680px; " + _
                                   "color: #000000; ' cellpadding='4' cellspacing='0'> " + _
                                   "<tr style='font-weight: bold; font-size:10pt; '><td style='width:25px;'>4</td><td>DIFICULTADES Y LIMITACIONES PRESENTADAS</td></tr>"
        strObservaciones = strObservaciones + "<tr style='vertical-align:top; font-size: 9pt; '><td></td><td><PRE style='font-size: 11pt; font-family:Times New Roman; text-align:justify'>" + txtObs.Text.ToString + "</PRE></td></tr>"
        strObservaciones = strObservaciones + "</table></div>"

        Dim strFirma As String = "<div style='width: 680px; text-align:center'>" + _
                                   "<table style='border-width: 1px 1px 1px 1px; width: 680px; " + _
                                   "color: #000000; ' cellpadding='4' cellspacing='0'> " + _
                                   "<tr style='font-weight: bold; font-size:9pt;'><td style='width:80px'></td><td style='text-align:center; width:220px; border-top: solid 1px #000000;'>NOMBRE</td><td style='width:80px'></td><td style='text-align:center; width:220px; border-top: solid 1px #000000;'>FIRMA</td><td style='width:80px'></td></tr>" + _
                                   "<tr style='font-weight: bold; font-size:9pt;'><td style='width:80px'></td><td style='text-align:left; width:220px;'></td>FECHA:&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp / &nbsp&nbsp  /  <td style='width:80px'></td><td style='text-align:center; width:220px;'></td><td style='width:80px'></td></tr>"
        strFirma = strFirma + "</table></div><br>"

        Dim strIndice As String = "<div style='width: 680px; text-align:center'>" + _
                                   "<table style='border-width: 1px 1px 1px 1px; border-style:solid; border-color:black; width: 240px; " + _
                                   "color: #000000; ' cellpadding='0' cellspacing='0'> " + _
                                   "<tr style='font-weight: bold; font-size:9pt;'><td style='text-align:center; width:120px; border-right:1px solid black'>Indice de Avance Físico Global (IAFG)</td><td style='text-align:center; width:120px;'></td></tr>" + _
                                   "</table></div>"
        Response.Clear()
        Response.Buffer = True

        Response.ContentType = "application/vnd.ms-word"
        Response.AddHeader("Content-Disposition", "attachment;filename=Informe_Mensual_(Formato_03)_" + Now.Date.Day.ToString + "_" + Now.Date.Month.ToString + "_" + Now.Date.Year.ToString + "_" + Now.Hour.ToString + "_" + Now.Minute.ToString + "_" + Now.Second.ToString + ".doc")
        'Response.AddHeader("Content-Disposition", "attachment;filename=AVANCE_FINANCIERO_SEMANAL_" + Now.Date.Year.ToString + Now.Date.Month.ToString + Now.Date.Day.ToString + Now.Hour.ToString + Now.Minute.ToString + Now.Second.ToString + ".xls")


        Response.Charset = "UTF-8"
        Response.ContentEncoding = Encoding.Default
        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As StringWriter = New StringWriter(sb)
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        Dim pagina As Page = New Page
        Dim form = New HtmlForm
        gv_ifinanciero.AllowPaging = False
        gv_ifinanciero.DataBind()
        pagina.EnableEventValidation = False
        pagina.DesignerInitialize()
        pagina.Controls.Add(form)
        form.Controls.Add(gvtitle1)
        form.Controls.Add(gv_ifinanciero)
        pagina.RenderControl(htw)

        Response.Output.Write(Cabecera.ToString)
        Response.Output.Write("<br>")
        Response.Output.Write(ChartTable1.ToString)
        Response.Output.WriteLine("<br>")
        Response.Output.Write(strComentarioFis.ToString)
        Response.Output.WriteLine("<br>")
        Response.Output.Write(ChartTable2.ToString)
        Response.Output.WriteLine("<br>")
        Response.Output.Write(strComentarioFin.ToString)
        Response.Output.WriteLine("<br>")
        Response.Output.Write(strObservaciones.ToString)
        Response.Output.WriteLine("<br>")
        Response.Output.WriteLine("<p style='text-decoration:underline; font-weight:bold; font-size:9pt'>ESPECIALISTA TECNICO</p><br><br><br>")
        Response.Output.WriteLine(strFirma)
        Response.Output.WriteLine("<p style='text-decoration:underline; font-weight:bold; font-size:9pt'>JEFE DE OFICINA ZONAL</p><br><br><br>")
        Response.Output.WriteLine(strFirma)
        Response.Output.WriteLine("<p style='text-decoration:underline; font-weight:bold; font-size:9pt'>ESPECIALISTA PPER</p>")
        Response.Output.WriteLine(strIndice + "<br><br><br><br><br><br>")
        Response.Output.WriteLine(strFirma)
        Response.Flush()
        Response.End()

    End Sub

#End Region

    Protected Sub TabContainer1_ActiveTabChanged(sender As Object, e As EventArgs) Handles TabContainer1.ActiveTabChanged
        If TabContainer1.ActiveTabIndex = 1 Then
            txtObs.Focus()
        Else
            txtObs.Focus()
        End If
    End Sub

End Class
